package com.suxin.netty.coder;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author Tang
 * @classname NioClient
 * @description [ TODO ]
 * @date 2021/1/8 21:42
 */
public class NioClient {

    public static void main(String[] args) {

        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(workGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            // 出站的编码器
                            pipeline.addLast(new MyOutEncodeInt());
                            pipeline.addLast(new MyClientHandler());

                            // 入站的解码器
                            pipeline.addLast(new MyInDecoderInt());
                        }
                    });
            System.out.println("client ready...");
            ChannelFuture future = bootstrap.connect("127.0.0.1", 9000).sync();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
            workGroup.shutdownGracefully();
        }
    }

}